<template>
  <div class="app-container">
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="listQuery.Filter" clearable size="small" placeholder="搜索..." style="width: 200px;"
        class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search"
        @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus"
        @click="handleCreate">新增</el-button>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-edit"
        @click="handleUpdate()">修改</el-button>
      <el-button slot="reference" class="filter-item" type="danger" icon="el-icon-delete" size="mini"
        @click="handleDelete()">删除</el-button>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="cancel()"
      :title="formTitle">
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="采购单价" prop="purchaseUnitprice">
          <el-input v-model="form.purchaseUnitprice" placeholder="请输入采购单价" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="预付金额" prop="prepaymentAmount">
          <el-input v-model="form.prepaymentAmount" placeholder="请输入预付金额" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="采购数量" prop="quantityPurchased">
          <el-input v-model="form.quantityPurchased" placeholder="请输入采购数量" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label=" 规格型号" prop="specificationModel">
          <el-input v-model="form.specificationModel" placeholder="请输入 规格型号" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="采购人员" prop="purchasingStaff">
          <el-input v-model="form.purchasingStaff" placeholder="请输入采购人员" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="采购单号" prop="purchaseOrdernumber">
          <el-input v-model="form.purchaseOrdernumber" placeholder="请输入采购单号" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <el-input v-model="form.image" placeholder="请输入图片" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label=" 采购状态" prop="procurementStatus">
          <el-input v-model="form.procurementStatus" placeholder="请输入 采购状态" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="采购运费" prop="procurementFreight">
          <el-input v-model="form.procurementFreight" placeholder="请输入采购运费" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="折后单价" prop="afterFoldingprice">
          <el-input v-model="form.afterFoldingprice" placeholder="请输入折后单价" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label=" 采购总额" prop="procurementTotal">
          <el-input v-model="form.procurementTotal" placeholder="请输入 采购总额" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="其他运费" prop="otherFreight">
          <el-input v-model="form.otherFreight" placeholder="请输入其他运费" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="折扣率" prop="discountedRate">
          <el-input v-model="form.discountedRate" placeholder="请输入折扣率" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="供应商" prop="vendorID">
          <el-input v-model="form.vendorID" placeholder="请输入供应商" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="生产日期" prop="dateOfmanufacture">
          <el-input v-model="form.dateOfmanufacture" placeholder="请输入生产日期" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="采购日期" prop="dateOfpurchase">
          <el-input v-model="form.dateOfpurchase" placeholder="请输入采购日期" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="预计到货日期" prop="estimatedArrivaldate">
          <el-input v-model="form.estimatedArrivaldate" placeholder="请输入预计到货日期" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="过期日期" prop="expirationDate">
          <el-input v-model="form.expirationDate" placeholder="请输入过期日期" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="商品 ID" prop="goodID">
          <el-input v-model="form.goodID" placeholder="请输入商品 ID" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="form.goodName" placeholder="请输入商品名称" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="仓库 ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库 ID" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label=" 区域 ID" prop="regionId">
          <el-input v-model="form.regionId" placeholder="请输入 区域 ID" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="库区 ID" prop="reservoirId">
          <el-input v-model="form.reservoirId" placeholder="请输入库区 ID" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="库位 ID" prop="warehouselocationId">
          <el-input v-model="form.warehouselocationId" placeholder="请输入库位 ID" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="small" type="text" @click="cancel">取消</el-button>
        <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
      </div>
    </el-dialog>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" size="small" style="width: 90%;"
      @sort-change="sortChange" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="44px"></el-table-column>
      <el-table-column label="采购单价" prop="purchaseUnitprice" align="center" />
      <el-table-column label="预付金额" prop="prepaymentAmount" align="center" />
      <el-table-column label="采购数量" prop="quantityPurchased" align="center" />
      <el-table-column label=" 规格型号" prop="specificationModel" align="center" />
      <el-table-column label="采购人员" prop="purchasingStaff" align="center" />
      <el-table-column label="采购单号" prop="purchaseOrdernumber" align="center" />
      <el-table-column label="品牌" prop="brand" align="center" />
      <el-table-column label="图片" prop="image" align="center" />
      <el-table-column label=" 采购状态" prop="procurementStatus" align="center" />
      <el-table-column label="采购运费" prop="procurementFreight" align="center" />
      <el-table-column label="折后单价" prop="afterFoldingprice" align="center" />
      <el-table-column label="备注" prop="remark" align="center" />
      <el-table-column label=" 采购总额" prop="procurementTotal" align="center" />
      <el-table-column label="其他运费" prop="otherFreight" align="center" />
      <el-table-column label="折扣率" prop="discountedRate" align="center" />
      <el-table-column label="供应商" prop="vendorID" align="center" />
      <el-table-column label="生产日期" prop="dateOfmanufacture" align="center" />
      <el-table-column label="采购日期" prop="dateOfpurchase" align="center" />
      <el-table-column label="预计到货日期" prop="estimatedArrivaldate" align="center" />
      <el-table-column label="过期日期" prop="expirationDate" align="center" />
      <el-table-column label="商品 ID" prop="goodID" align="center" />
      <el-table-column label="商品名称" prop="goodName" align="center" />
      <el-table-column label="仓库 ID" prop="warehouseId" align="center" />
      <el-table-column label=" 区域 ID" prop="regionId" align="center" />
      <el-table-column label="库区 ID" prop="reservoirId" align="center" />
      <el-table-column label="库位 ID" prop="warehouselocationId" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)" icon="el-icon-edit" />
          <el-button type="danger" size="mini" @click="handleDelete(row)" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount>0" :total="totalCount" :page.sync="page"
      :limit.sync="listQuery.MaxResultCount" @pagination="getList" />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
const defaultForm = {
  id: null,
  purchaseUnitprice: null,
  prepaymentAmount: null,
  quantityPurchased: null,
  specificationModel: null,
  purchasingStaff: null,
  purchaseOrdernumber: null,
  brand: null,
  image: null,
  procurementStatus: null,
  procurementFreight: null,
  afterFoldingprice: null,
  remark: null,
  procurementTotal: null,
  otherFreight: null,
  discountedRate: null,
  vendorID: null,
  dateOfmanufacture: null,
  dateOfpurchase: null,
  estimatedArrivaldate: null,
  expirationDate: null,
  goodID: null,
  goodName: null,
  warehouseId: null,
  regionId: null,
  reservoirId: null,
  warehouselocationId: null,
}
export default {
  name: 'purchase',
  components: {
    Pagination
  },
  directives: {
    permission
  },
  props: [],
  data() {
    return {
      rules: {
        purchaseUnitprice: [{
          required: true,
          message: '请输入采购单价',
          trigger: 'blur'
        }],
        prepaymentAmount: [{
          required: true,
          message: '请输入预付金额',
          trigger: 'blur'
        }],
        quantityPurchased: [{
          required: true,
          message: '请输入采购数量',
          trigger: 'blur'
        }],
        specificationModel: [{
          required: true,
          message: '请输入 规格型号',
          trigger: 'blur'
        }],
        purchasingStaff: [{
          required: true,
          message: '请输入采购人员',
          trigger: 'blur'
        }],
        purchaseOrdernumber: [{
          required: true,
          message: '请输入采购单号',
          trigger: 'blur'
        }],
        brand: [{
          required: true,
          message: '请输入品牌',
          trigger: 'blur'
        }],
        image: [{
          required: true,
          message: '请输入图片',
          trigger: 'blur'
        }],
        procurementStatus: [{
          required: true,
          message: '请输入 采购状态',
          trigger: 'blur'
        }],
        procurementFreight: [{
          required: true,
          message: '请输入采购运费',
          trigger: 'blur'
        }],
        afterFoldingprice: [{
          required: true,
          message: '请输入折后单价',
          trigger: 'blur'
        }],
        remark: [{
          required: true,
          message: '请输入备注',
          trigger: 'blur'
        }],
        procurementTotal: [{
          required: true,
          message: '请输入 采购总额',
          trigger: 'blur'
        }],
        otherFreight: [{
          required: true,
          message: '请输入其他运费',
          trigger: 'blur'
        }],
        discountedRate: [{
          required: true,
          message: '请输入折扣率',
          trigger: 'blur'
        }],
        vendorID: [{
          required: true,
          message: '请输入供应商',
          trigger: 'blur'
        }],
        dateOfmanufacture: [{
          required: true,
          message: '请输入生产日期',
          trigger: 'blur'
        }],
        dateOfpurchase: [{
          required: true,
          message: '请输入采购日期',
          trigger: 'blur'
        }],
        estimatedArrivaldate: [{
          required: true,
          message: '请输入预计到货日期',
          trigger: 'blur'
        }],
        expirationDate: [{
          required: true,
          message: '请输入过期日期',
          trigger: 'blur'
        }],
        goodID: [{
          required: true,
          message: '请输入商品 ID',
          trigger: 'blur'
        }],
        goodName: [{
          required: true,
          message: '请输入商品名称',
          trigger: 'blur'
        }],
        warehouseId: [{
          required: true,
          message: '请输入仓库 ID',
          trigger: 'blur'
        }],
        regionId: [{
          required: true,
          message: '请输入 区域 ID',
          trigger: 'blur'
        }],
        reservoirId: [{
          required: true,
          message: '请输入库区 ID',
          trigger: 'blur'
        }],
        warehouselocationId: [{
          required: true,
          message: '请输入库位 ID',
          trigger: 'blur'
        }],
      },
      form: Object.assign({}, defaultForm),
      list: null,
      totalCount: 0,
      listLoading: true,
      formLoading: false,
      listQuery: {
        Filter: '',
        Sorting: '',
        SkipCount: 0,
        MaxResultCount: 10
      },
      page: 1,
      dialogFormVisible: false,
      multipleSelection: [],
      formTitle: '',
      isEdit: false,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    getList() {
      this.listLoading = true;
      this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios.gets('/api/business/Purchase', this.listQuery).then(response => {
        this.list = response.items;
        this.totalCount = response.totalCount;
        this.listLoading = false;
      });
    },
    fetchData(id) {
      this.$axios.gets('/api/business/Purchase/' + id).then(response => {
        this.form = response;
      });
    },
    handleFilter() {
      this.page = 1;
      this.getList();
    },
    handleCreate() {
      this.formTitle = '新增采购计划表';
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      var params = [];
      let alert = '';
      if (row) {
        params.push(row.id);
        alert = row.name;
      }
      else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: '未选择',
            type: 'warning'
          });
          return;
        }
        this.multipleSelection.forEach(element => {
          let id = element.id;
          params.push(id);
        });
        alert = '选中项';
      }
      this.$confirm('是否删除' + alert + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.posts('/api/business/Purchase/delete', params).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleUpdate(row) {
      this.formTitle = '修改采购计划表';
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
        this.dialogFormVisible = true;
      }
      else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: '编辑必须选择单行',
            type: 'warning'
          });
          return;
        }
        else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogFormVisible = true;
        }
      }
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) {
            this.$axios.posts('/api/business/Purchase/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
          else {
            this.$axios.posts('/api/business/Purchase/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
        }
      });
    },
    sortChange(data) {
      const {
        prop,
        order
      } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + ' ' + order;
      this.handleFilter();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    },
  }
}

</script>
<style>
</style>
